package com.gci.lib_markdown_editor

import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import java.io.File
import java.io.FileOutputStream



fun Uri.getRealPath(context: Context):String{
    var ret: String
    val cursor = context.contentResolver.query(
            this,
            arrayOf(MediaStore.Images.ImageColumns.DATA),
            null,null,null
    )
    if (cursor == null){
        ret = this.path!!
    }
    else{
        cursor.moveToFirst()
        val index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
        ret = cursor.getString(index)
    }
    return ret
}

fun Bitmap.save(filename:String):String{
    val outputDir = Environment.getExternalStorageDirectory().path+"/SPlayer/note_images"
    val dirFile = File(outputDir)
    if (!dirFile.exists()){
        dirFile.mkdirs()
    }

    val outputFile = File(dirFile,"$filename.jpg")
    if (!outputFile.exists())
        outputFile.createNewFile()

    val fos = FileOutputStream(outputFile)
    this.compress(Bitmap.CompressFormat.JPEG,100,fos)
    fos.flush()
    fos.close()
    return "$outputDir/$filename.jpg"
}

val isKeyboardConnected = { activity: Activity ->
    activity.resources.configuration.keyboard == Configuration.KEYBOARD_QWERTY
}